#!/usr/bin/env python3
"""
游戏角色收藏管理器 - 单文件简化版
避免模块导入问题
"""

import tkinter as tk
from tkinter import messagebox, simpledialog
import json
import os
import uuid

class SimpleCharacterManager:
    def __init__(self):
        self.root = tk.Tk()
        self.setup_window()
        self.data_file = "characters.json"
        self.characters = self.load_data()
        self.setup_ui()
    
    def setup_window(self):
        self.root.title("🎮 game character manager")
        self.root.geometry("700x500")
        self.root.configure(bg="#2c3e50")
        self.center_window()
    
    def center_window(self):
        self.root.update_idletasks()
        width = self.root.winfo_width()
        height = self.root.winfo_height()
        x = (self.root.winfo_screenwidth() // 2) - (width // 2)
        y = (self.root.winfo_screenheight() // 2) - (height // 2)
        self.root.geometry(f'{width}x{height}+{x}+{y}')
    
    def load_data(self):
        if os.path.exists(self.data_file):
            try:
                with open(self.data_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except:
                return []
        return []
    
    def save_data(self):
        with open(self.data_file, 'w', encoding='utf-8') as f:
            json.dump(self.characters, f, ensure_ascii=False, indent=2)
    
    def setup_ui(self):
        # 标题
        title_frame = tk.Frame(self.root, bg="#2c3e50")
        title_frame.pack(fill="x", pady=20)
        
        title_label = tk.Label(title_frame, text="🎮game character manager ", 
                              font=("Arial", 20, "bold"), fg="#ecf0f1", bg="#2c3e50")
        title_label.pack()
        
        subtitle_label = tk.Label(title_frame, text="manage your team,up to 4 members",
                                font=("Arial", 12), fg="#bdc3c7", bg="#2c3e50")
        subtitle_label.pack()
        
        # 统计信息
        self.stats_label = tk.Label(title_frame, text=f"numbers: {len(self.characters)}/4",
                                   font=("Arial", 14, "bold"), fg="#e74c3c", bg="#2c3e50")
        self.stats_label.pack(pady=10)
        
        # 按钮框架
        button_frame = tk.Frame(self.root, bg="#2c3e50")
        button_frame.pack(pady=10)
        
        tk.Button(button_frame, text="➕ adding characters", command=self.add_character,
                 bg="#27ae60", fg="white", font=("Arial", 12), 
                 width=15, height=2).pack(side="left", padx=10)
        
        tk.Button(button_frame, text="🔄 refresh", command=self.refresh_display,
                 bg="#3498db", fg="white", font=("Arial", 12),
                 width=10, height=2).pack(side="left", padx=10)
        
        tk.Button(button_frame, text="❌ exit", command=self.root.quit,
                 bg="#e74c3c", fg="white", font=("Arial", 12),
                 width=10, height=2).pack(side="left", padx=10)
        
        # 角色显示区域
        self.chars_frame = tk.Frame(self.root, bg="#34495e")
        self.chars_frame.pack(fill="both", expand=True, padx=20, pady=10)
        
        self.refresh_display()
    
    def add_character(self):
        if len(self.characters) >= 4:
            messagebox.showwarning("hint", "4character only!")
            return
        
        # 使用简单对话框获取输入
        name = simpledialog.askstring("Adding characters", "name:")
        if not name:
            return
        
        char_class = simpledialog.askstring("adding characters", "input position:", initialvalue="croissant")
        if not char_class:
            return
        
        level = simpledialog.askinteger("Adding characters", "input level (1-100):", minvalue=1, maxvalue=100)
        if not level:
            return
        
        # 创建新角色
        new_character = {
            "id": str(uuid.uuid4())[:8],
            "name": name,
            "type": char_class,
            "level": level
        }
        
        self.characters.append(new_character)
        self.save_data()
        self.refresh_display()
        
        messagebox.showinfo("success", f" {name} successfully adding!")
    
    def delete_character(self, character):
        if messagebox.askyesno("make sure to delete", f"Are you sure to delete {character['name']} ?\nthis operation is not irrevocable!"):
            self.characters = [c for c in self.characters if c['id'] != character['id']]
            self.save_data()
            self.refresh_display()
            messagebox.showinfo("success", f" {character['name']} has been deleted")
    
    def refresh_display(self):
        # 清空显示区域
        for widget in self.chars_frame.winfo_children():
            widget.destroy()
        
        # 更新统计信息
        self.stats_label.config(text=f"number of characters: {len(self.characters)}/4")
        
        if not self.characters:
            empty_label = tk.Label(self.chars_frame, text="🎯 no one is here...\nclick 'adding characters' to create your first hero!",
                                  font=("Arial", 14), fg="#7f8c8d", bg="#34495e",
                                  justify="center")
            empty_label.pack(expand=True)
            return
        
        # 显示角色卡片
        for i, character in enumerate(self.characters):
            card = self.create_character_card(character)
            card.pack(fill="x", pady=5, padx=10)
    
    def create_character_card(self, character):
        card = tk.Frame(self.chars_frame, bg="#ecf0f1", relief="raised", bd=2)
        
        # 角色信息
        info_frame = tk.Frame(card, bg="#ecf0f1")
        info_frame.pack(fill="x", padx=10, pady=8)
        
        char_info = tk.Label(info_frame, 
                            text=f"🏷️ {character['name']} | ⚔️ {character['type']} | ⭐ level {character['level']}",
                            font=("Arial", 11, "bold"), bg="#ecf0f1", fg="#2c3e50")
        char_info.pack(side="left")
        
        # 删除按钮
        delete_btn = tk.Button(info_frame, text="🗑️ deleting", 
                              command=lambda: self.delete_character(character),
                              bg="#e74c3c", fg="white", font=("Arial", 9))
        delete_btn.pack(side="right")
        
        return card
    
    def run(self):
        self.root.mainloop()

if __name__ == "__main__":
    app = SimpleCharacterManager()
    app.run()